<template>
  <div class="category-container">
    <div style="margin-top: 5px; color: #606266; font-size: 15px">
      <!-- 搜索区域 -->
      <el-button
        size="small"
        type="primary"
        @click="showAddCateDialog"
      ><i class="el-icon-plus" />&nbsp;新增</el-button>
      <span style="padding: 0 10px">知识类别：</span>
      <!-- 绑定-刷新-重置 -->
      <el-input
        v-model="queryInfo.knowCateName"
        size="small"
        placeholder="请输入知识类别"
        clearable
        @keyup.enter.native="getCateList"
        @clear="getCateList"
      >
        <el-button
          slot="append"
          size="small"
          icon="el-icon-search"
          @click="getCateList()"
        />
      </el-input>
    </div>

    <!-- 列表区域 -->
    <div style="margin-top: 10px">
      <tree-table
        :data="cateList"
        border
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-row-hover="false"
      >
        <template slot="opt" slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            icon="el-icon-edit"
            @click="showEditCateDialog(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            icon="el-icon-plus"
            @click="showAddLittleDialog(scope.row)"
          >添加子类别</el-button>
          <el-button
            size="mini"
            type="danger"
            plain
            icon="el-icon-delete"
            @click="removeCateByID(scope.row)"
          >删除</el-button>
        </template>
      </tree-table>
    </div>

    <!-- 分页功能 -->
    <div style="padding-top: 10px">
      <el-pagination
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryInfo.pageSize"
        layout="sizes, total,  prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 新增知识类别的对话框 -->
    <el-dialog
      title="新增知识类别"
      :visible.sync="addCateDialogVisible"
      width="40%"
    >
      <el-form
        ref="addCateForm"
        :model="addCateForm"
        :rules="addCateFormRules"
        label-width="auto"
      >
        <el-form-item label="知识类别名称" prop="knowCateName">
          <el-input v-model="addCateForm.knowCateName" clearable />
        </el-form-item>
        <el-form-item label="分类描述" prop="notes">
          <el-input v-model="addCateForm.notes" clearable />
        </el-form-item>
        <!-- 创建者是否直接引入用户名 -->
        <!-- <el-form-item label="创建者" prop="staffCode">
          <el-input v-model="addCateForm.staffCode" :readonly="true" />
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑知识类别的对话框 -->
    <el-dialog
      title="编辑知识类别"
      :visible.sync="editCateDialogVisible"
      width="40%"
    >
      <el-form
        ref="editCateForm"
        :model="editCateForm"
        :rules="addCateFormRules"
        label-width="auto"
      >
        <el-form-item label="知识类别名称" prop="knowCateName">
          <el-input v-model="editCateForm.knowCateName" clearable />
        </el-form-item>
        <el-form-item label="分类描述" prop="notes">
          <el-input v-model="editCateForm.notes" clearable />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加子类别的对话框 -->
    <el-dialog
      title="添加子类别"
      :visible.sync="addLittleDialogVisible"
      width="40%"
    >
      <el-form
        ref="addLittleForm"
        :model="addLittleForm"
        :rules="addCateFormRules"
        label-width="auto"
      >
        <el-form-item label="父类名称">
          <el-input v-model="addLittleForm.parentName" disabled />
        </el-form-item>
        <el-form-item label="知识类别名称" prop="knowCateName">
          <el-input v-model="addLittleForm.knowCateName" clearable />
        </el-form-item>
        <el-form-item label="分类描述" prop="notes">
          <el-input v-model="addLittleForm.notes" clearable />
        </el-form-item>
        <!-- 创建者是否直接引入用户名 -->
        <!-- <el-form-item label="创建者" prop="staffCode">
          <el-input
            v-model="addLittleForm.staffCode"
            :readonly="true"
          />
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addLittleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addLittleCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- </el-main> -->
  </div>
</template>

<script>
import { getAllApi, getSingleByIdApi, cateInsertApi, cateUpdateApi, cateDeleteByIDApi } from '@/api/knowMan'
export default {
  data() {
    return {
      cateList: [],
      // 查询条件
      queryInfo: {
        pageNum: 1, // 页码
        pageSize: 10 // 每页显示多少条数据
      },
      total: 0,
      columns: [
        {
          label: '知识类别',
          prop: 'knowCateName'
        },
        {
          label: '分类描述',
          prop: 'notes'
        },
        {
          label: '创建者',
          prop: 'staffCode'
        },
        {
          label: '创建时间',
          prop: 'createtime'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
          width: '300'
        }
      ],
      // todo 新增分类
      addCateDialogVisible: false,
      addCateForm: {
        knowCateName: '',
        notes: ''
      },
      addCateFormRules: {
        knowCateName: [
          { required: true, message: '请输入知识分类名称', trigger: 'blur' }
        ]
      },

      // todo 编辑分类
      editCateDialogVisible: false,
      editCateForm: {},

      // todo 添加子类别
      addLittleDialogVisible: false,
      addLittleForm: {
        parentId: '',
        knowCateName: '',
        notes: ''
      },
      staffCode: ''
    }
  },
  created() {
    this.userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
    this.staffCode = this.userInfo.username ? this.userInfo.username : this.userInfo.staffCode
    this.getCateList()
  },
  methods: {
    getCateList() {
      getAllApi(this.queryInfo).then((res) => {
        if (res.code === 200) {
          this.cateList = res.list.records
          this.total = res.list.total
        }
      })
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.getCateList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage
      this.getCateList()
    },

    // todo 新增分类
    showAddCateDialog() {
      this.addCateDialogVisible = true
      // ! 创建者暂时设置一个默认值，后续和用户模块绑定
      this.addCateForm.staffCode = this.staffCode
      if (this.$refs.addCateForm !== undefined) {
        this.$refs.addCateForm.resetFields()
      }
    },
    async addCate() {
      this.$refs.addCateForm.validate(async(valid, field) => {
        if (!valid) {
          const arr = Object.keys(field)
          return this.$message.error({
            message: field[arr[0]][0].message,
            druation: 1500
          })
        }
        cateInsertApi(this.addCateForm).then((res) => {
          if (res.code === 200) {
            this.addCateDialogVisible = false
            this.getCateList()
          }
        })
      })
    },

    // todo 编辑分类
    showEditCateDialog(cateInfo) {
      const param = {
        knowCateId: cateInfo.knowCateId
      }
      getSingleByIdApi(param).then((res) => {
        if (res.code === 200) {
          this.editCateForm = res.single
          this.editCateDialogVisible = true
          if (this.$refs.editCateForm !== undefined) {
            this.$refs.editCateForm.resetFields()
          }
        }
      })
    },

    async editCate() {
      this.$refs.editCateForm.validate(async(valid, field) => {
        if (!valid) {
          const arr = Object.keys(field)
          return this.$message.error({
            message: field[arr[0]][0].message,
            druation: 1500
          })
        }

        const param = {
          knowCateId: this.editCateForm.knowCateId, knowCateName: this.editCateForm.knowCateName, notes: this.editCateForm.notes
        }
        cateUpdateApi(param).then((res) => {
          if (res.code === 200) {
            this.editCateDialogVisible = false
            this.getCateList()
          }
        })
      })
    },

    // todo 添加子类别
    async showAddLittleDialog(cateInfo) {
      const param = {
        knowCateId: cateInfo.knowCateId
      }
      getSingleByIdApi(param).then((res) => {
        if (res.code === 200) {
          this.addLittleForm.parentId = res.single.knowCateId
          this.addLittleForm.parentName = res.single.knowCateName
          if (res.single.cateId.length / 2 < 3) {
            this.addLittleDialogVisible = true
            // ! 创建者暂时设置一个默认值，后续和用户模块绑定
            this.addLittleForm.staffCode = this.staffCode
            if (this.$refs.addLittleForm !== undefined) {
              this.$refs.addLittleForm.resetFields()
            }
            return
          }
          this.addLittleDialogVisible = false
          return this.$message.warning({
            message: '已经是最小类，不能再新增子类！',
            druation: 1500
          })
        }
      })
    },

    async addLittleCate() {
      this.$refs.addLittleForm.validate(async(valid, field) => {
        if (!valid) {
          const arr = Object.keys(field)
          return this.$message.error({
            message: field[arr[0]][0].message,
            druation: 1500
          })
        }
        const param = {
          parentId: this.addLittleForm.parentId,
          knowCateName: this.addLittleForm.knowCateName,
          notes: this.addLittleForm.notes,
          staffCode: this.addLittleForm.staffCode
        }

        cateInsertApi(param).then((res) => {
          if (res.code === 200) {
            this.addLittleDialogVisible = false
            this.getCateList()
          }
        })
      })
    },
    // todo 删除分类
    async removeCateByID(cateInfo) {
      const result = await this.$confirm(`确定要删除该条记录？`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).catch(reason => reason)
      // 取消删除
      if (result === 'cancel') {
        return this.$message.info({
          message: '已取消删除!',
          druation: 1500
        })
      }
      const param = {
        knowCateId: cateInfo.knowCateId
      }
      cateDeleteByIDApi(param).then((res) => {
        if (res.code === 200) {
          this.getCateList()
          this.$message.success({
            message: '删除成功!',
            druation: 1000
          })
        }
        console.log(111, res)
      }).catch(() => {
        this.$message.error({ message: '该类下还存在子类，不能删除' })
      })
    }
  }
}
</script>

<style scoped>
.category-container {
  padding: 15px;
}

.el-header {
  padding: 0 40px;
  background-color: #fff;
}

.category-search {
  display: flex;
  align-items: center;
  height: 60px;
}

.el-input {
  width: 300px;
}

.el-main {
  overflow: hidden !important;
  margin-top: 15px;
  background-color: #fff;
}

.el-button--mini {
  padding: 7px 3px;
  font-size: 10px;
}
</style>

<style>
.zk-table {
  color: #5d6880 !important;
  border-color: #e4ebf5 !important;
  border-left-width: 0 !important;
  border-right-width: 0 !important;
}

.zk-table__header-row,
.zk-table__body-row:hover {
  background-color: #f5f7fd !important;
}

.zk-table__cell-inner {
  padding-left: 50px !important;
}
div /deep/ .el-dialog {
  margin-top: 40px !important;
  border-radius: 8px;
}
div /deep/ .el-dialog__body {
  padding: 0 25px;
}
</style>
